const addressModel = require('../model/addressModel');

// 查看所有订单
module.exports.findAllAddress = async function (searchObj, page) {
    if (page.size) {
        //current当前页，size一页显示多少条数据
        let { current, size } = page;
        // 获取所有服务的总数
        let total = await addressModel.countDocuments(searchObj);
        // 获取总页数 Math.ceil:向上取整
        let totalPage = Math.ceil(total / size);
        // 当前页显示的数据
        let rows = await addressModel
            .find(searchObj)
            .populate({ path: 'userId' })
            .limit(parseInt(size))//条数
            .skip((current - 1) * size);
        return { current, size, total, totalPage, rows };
    } else {
        return await addressModel
            .find(searchObj)
            .populate({ path: 'userId' })
    }
}
// 新增地址
module.exports.addNewAddress = async function (addressObj){
    return await addressModel.create( addressObj );
}

// 根据 id 删除地址
module.exports.deleteAddress = async function (id){
    return await addressModel.deleteOne( { _id: id } );
}
